/***************************************************************************
*   Copyright (C) 2009 - Jared Kells <jkells@gmail.com>                   *
*   Copyright (C) 2010 - 小贱  huaihuaixiaoshao@gmail.com                  *
*                                                                         *
*   This program is free software; you can redistribute it and/or modify  *
*   it under the terms of the GNU General Public License as published by  *
*   the Free Software Foundation; either version 2 of the License, or     *
*   (at your option) any later version.                                   *
***************************************************************************/

#include "konsoleapplet.h"

#include <QGraphicsProxyWidget>
#include <QGraphicsLinearLayout>
#include <QAction>

#include <KMessageBox>


K_EXPORT_PLASMA_APPLET(konsolator, KonsoleApplet)

using namespace KParts;

KonsoleApplet::KonsoleApplet(QObject *parent, const QVariantList &args)
: Plasma::Applet(parent, args), m_konsolePart(0), m_partManager(0)
{ 

  setBackgroundHints(NoBackground);
  setHasConfigurationInterface( false );
	QAction *aboutAction = new QAction ( i18n ( "&About" ), this );
	aboutAction->setIcon ( KIcon ( "help-about" ) );
	connect ( aboutAction, SIGNAL(triggered()), this, SLOT(about()) );
	QAction *separator = new QAction ( this );
	separator->setSeparator ( true );
	m_actions.append ( aboutAction );
	m_actions.append ( separator );
	
	m_aboutData = new KAboutData ( "plasma_applet_konsolator",
		QByteArray (), ki18n ( "Konsolator" ), "0.2",
		ki18n ( "Konsole  Embedded In Plasma" ),
		KAboutData::License_GPL,
		ki18n ( "Copyright (C) 2010 小贱\n"
		       "Copyright (C) 2009 Jared Kells" ),
		ki18n ( "This plasmoid lets you konsole embedded in plasma." ));
	m_aboutData->addAuthor ( ki18n ( "小贱" ),
		ki18n ( "Developer" ), "huaihuaixiaoshao@gmail.com" );
	m_aboutData->addCredit ( ki18n ( "Jared Kells" ),
		ki18n ( "For having this great idea :-)" ), "jkells@gmail.com" );
	m_aboutData->setProgramIconName ( "terminal" );
	m_aboutData->setTranslator ( ki18nc ( "NAME OF THE TRANSLATORS", "Your names" ),
		ki18nc ( "EMAIL OF THE TRANSLATORS", "Your emails" ) );

}

KonsoleApplet::~KonsoleApplet()
{
  saveConfiguration ();
  qDebug() << "config option saved";
  delete m_aboutData;

}

void KonsoleApplet::about() const
{
	KAboutApplicationDialog *aboutDialog = new KAboutApplicationDialog ( m_aboutData );
	connect ( aboutDialog, SIGNAL ( finished () ), aboutDialog, SLOT ( deleteLater () ) );
	aboutDialog->show ();
}

void KonsoleApplet::loadConfiguration()
{
  KConfigGroup cfg = config ();
  m_geometry.setWidth(cfg.readEntry("jheight", 300));
  m_geometry.setHeight(cfg.readEntry("jwidth", 300));


}

void KonsoleApplet::saveConfiguration()
{
  
  KConfigGroup cfg = config ();
  cfg.writeEntry ("jheight", m_geometry.height());
  cfg.writeEntry ( "jwidth", m_geometry.width() );




}

void KonsoleApplet::resizeEvent(QGraphicsSceneResizeEvent* event)
{

    m_geometry.setHeight( event->newSize().height() );
    m_geometry.setWidth(event->newSize().width());
    qDebug()<< " height  size is " << m_geometry.height();
    qDebug()<< " width  size is " << m_geometry.width();


    saveConfiguration ();
}



void KonsoleApplet::init()
{
    QGraphicsLinearLayout *layout = new QGraphicsLinearLayout();
    QGraphicsProxyWidget *proxy = new QGraphicsProxyWidget(this);
    layout->addItem( proxy );
    setLayout(layout); 
    
    loadConfiguration ();
    
    resize(m_geometry.height(), m_geometry.width());
    setAspectRatioMode(Plasma::IgnoreAspectRatio);
    setupTerm(proxy);
    connect( m_konsolePart, SIGNAL( destroyed() ),
	     this, SLOT( init() ));
    qDebug()<< " setup term  done";
}

bool KonsoleApplet::setupTerm(QGraphicsProxyWidget *proxy)
{
    qDebug() << "call setupTerm";
    
    KService::Ptr service = KService::serviceByDesktopName("konsolepart");
    
    if( !service.isNull() )
    {
	KPluginFactory *factory = KPluginLoader(*service.data()).factory();
	if( factory )
	{
	    if ( ( m_konsolePart = factory->create<KParts::ReadOnlyPart>( 0 ) ) )
	    {
		proxy->setWidget( m_konsolePart->widget() );		
		
		//Start Terminal
		TerminalInterface* interface = qobject_cast<TerminalInterface*>(m_konsolePart);
		Q_ASSERT(interface);
		interface->showShellInDir( QString() );
		
		// get focus on this widget
		bool yes = m_konsolePart->widget()->hasFocus();
		if (!yes) 
		{
		    qDebug() << "no focus on this widget set it now";
		    m_konsolePart->widget()->setFocus();
		}

	    }
	}
    }
    else
    {
	KMessageBox::error(0,"service konsolepart not found","konsole-plasmoid");
	return false;
    }
    
    return true;
}

void KonsoleApplet::mydebug()
{
    qDebug() << "call debug";
    
}

#include "konsoleapplet.moc"

